<template>
  <div>
    <el-menu mode="vertical" ref="menume" default-active="1-4-1" :show-timeout="0" background-color="#304156"
             text-color="#bfcbd9"
             active-text-color="#409EFF" @open="handleOpen" :collapse="isCollapse">
      <div class="logo">
        <!-- 111 -->
        <img src="../../assets/images/logo.png" alt="">
      </div>
      <div class="search">
        <el-input prefix-icon="el-icon-search" v-model="searchText" placeholder="请输入名称和URL">
        </el-input>
      </div>

      <!--  渲染list  -->
      <el-submenu v-for="(item,index) in list" :key="index" :index="index +''"
                  v-if="content&&content.$parent.$checkRoles([40002])">
        <template slot="title">
          <i class="el-icon-menu"></i>
          <span slot="title">{{ item.controller_comment }}</span>
        </template>
        <el-menu-item @click="chooseSid(items)" v-for="(items,indexs) in item.children" :key="indexs"
                      :index="index+'-'+indexs"><i @click.stop="deleteId(item,items)" class="el-icon-delete"></i>
          {{ items.title }}
        </el-menu-item>
        <el-menu-item v-if="content&&content.$parent.$checkRoles([40101])">
          <el-button type="primary" @click="add(item)" size="mini">导入接口</el-button>
        </el-menu-item>
      </el-submenu>

      <!-- <el-menu-item @click="chooseSid(items)" v-for="(items,indexs) in item.children" :key="indexs" :index="index+'-'+indexs">{{items.title}}</el-menu-item> -->
    </el-menu>
    <el-dialog :before-close="close" append-to-body title="导入接口" :visible.sync="show">
      <el-form label-position="right" label-width="100px">
        <el-form-item label="">
          <el-input type="textarea" placeholder="请输入内容" :rows="5" :autosize="{ minRows: 10, maxRows: 4}"
                    v-model="excilVal"/>
        </el-form-item>
        <el-form-item label="">
          <el-button type="success" @click="addRestful()" size="small">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {POST} from '@/api/request'
import {Message} from "element-ui";
import {requestData} from "../../api"; //request
export default {
  name: 'sidebar',
  data() {
    return {
      isCollapse: false,
      chooseList: [],
      searchText: '',
      timeout: null,
      listData: {},
      excilVal: null,
      show: false,
      newKy: null,
      content: {}
    }
  },
  props: {
    /**
     *  @list    接口导航栏数据
     */
    list: {
      type: Array
    }
  },
  components: {},

  created() {
    this.content = this
    let that = this
  },
  watch: {
    searchText(curVal, oldVal) {
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        // console.log(curVal)
        if (curVal) {
          this.$emit('getControllers', curVal)
        } else {
          this.$emit('getControllers')
        }
      }, 300);
    },
    list(curVal, oldVal) {
      if (oldVal.length === 0) {
        for (let i = 0; i < curVal.length; i++) {
          this.handleOpen(i + "", i)
        }
      }
    }
  },
  mounted: function () {
  },
  computed: {},
  methods: {
    rightClick() {   //鼠标右键
      // console.log(111)
    },
    close() {
      this.show = false
      this.excilVal = null
    },
    add(item) {
      this.listData = item
      this.show = true
    },
    //新增接口
    async deleteId(rows, row) {
      this.listData = rows
      this.$confirm('此操作将永久删除该接口吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let data = {
          controllerName: row.controller_name,
          requestUrl: row.request_url
        }
        await POST('/delInterfaces', data)
        let res = await POST('/getInterfaces', {controllerName: this.listData.controller_java_name})
        let sendData = {
          index: this.newKy * 1,
          children: res
        }
        // console.log(sendData)
        this.$emit('openList', sendData)
        this.$message.success('删除成功')
      }).catch(() => {
        // console.log('异常抛出')
      });
    },
    async addRestful() {
      // console.log(JSON.parse(this.excilVal))
      let data = JSON.parse(this.excilVal)
      for (let item in data) {
        if (data[item] == null) {
          data[item] = ''
        }
      }
      // console.log(data,222)
      let newData = await POST('/addInterfaces', {json: JSON.stringify(data)})
      let res = await POST('/getInterfaces', {controllerName: this.listData.controller_java_name})
      let sendData = {
        index: this.newKy * 1,
        children: res
      }
      this.$emit('openList', sendData)
      this.show = false
    },
    handleOpen(key, keyPath) {
      this.newKy = key
      if (this.list[key].children && this.list[key].children.length) return
      let data = {
        controllerName: this.list[key].controller_java_name
      }
      let res = POST('/getInterfaces', data)

      let that = this
      let result = requestData(2, "/getInterfaces", data).then(res => {
        let sendData = {
          index: key * 1,
          children: res
        }
        that.$emit('openList', sendData)
      }).catch(e => {

      })
    },
    handleClose(key, keyPath) {
      // console.log(key, keyPath);
    },
    chooseSid(item) {
      console.log(this.$parent.$meInfo)
      if (!(this.$parent.$checkRoles([20001]))) {
        this.$message.error("权限不足")
        return
      }
      this.$emit('chooseSid', item)
    },
    async getList(text) {
      let res = await POST('/findInterfaces', {text: text})
      // console.log(res)
    },
    // changeVal(val){
    //     console.log(111)
    // },
  }
}
</script>
<style lang="scss" scope>
.logo {
  width: 100%;
  height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;

  img {
    display: inline-block;
    width: 100px;
    height: 100px;
  }
}

.search {
  display: flex;
  justify-content: center;
  align-items: center;

  .el-input {
    width: 90%;

    input {
      border-radius: 20px;

    }
  }
}
</style>